package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbStudentCourse;
import com.beetlestech.nfe.main.biz.com.dto.CodeWithValueDTO;
import com.beetlestech.nfe.main.biz.com.dto.StudentCourseDTO;
import com.beetlestech.nfe.main.biz.com.dto.StudentCourseInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.view.VcourseDTO;
import com.beetlestech.nfe.main.db.view.VstudentcourseDTO;
import com.beetlestech.nfe.main.db.view.VstudentcoursegroupDTO;


public class HbStudentCourseImpl extends AbstractDBLogic implements HbStudentCourse, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程评价信息表";

	public StudentCourseDTO getStudentCourse(Integer studentID, Integer[] courseType){
		StudentCourseDTO studentCourse = new StudentCourseDTO();

		studentCourse.setCourseType(getCourseType(studentID));
		studentCourse.setStudentCourseInfoArray(getStudentCourseInfoArray(studentID, courseType));
		
		return studentCourse;
	}
	
	public void UpdateCourseWarmingReason(Integer orderID, Integer courseID, String warmingReason){
		OrderdetailDTO orderdetail = new OrderdetailDTO();
		orderdetail.setorderid(orderID);
		orderdetail.setgoodsid(courseID);
		orderdetail.setwarmingreason(warmingReason);
		orderdetail.setstate(Integer.parseInt(LEARNING_STATE.TO_THAW.toString()));
		updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetail, M10190WS, SH_INFO);
	}
	
	private StudentCourseInfoDTO[] getStudentCourseInfoArray(Integer studentID, Integer[] courseType){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VstudentcourseDTO.COLUMN_STUDENT_ID, StatementUtil.KEYWORD.EQUAL, studentID);
		StringBuffer sbCourse = new StringBuffer();
		
		if(courseType!=null){
			sbCourse.append(SQLConstant.SQL_AND);
			sbCourse.append(SQLConstant.BRACKET_LEFT);
			
			StatementDTO tempDTO = new StatementDTO();
			
			for(Integer item: courseType){
				StatementUtil.orWhere(tempDTO, VcourseDTO.COLUMN_COURSETYPE_C, StatementUtil.KEYWORD.EQUAL, item);
			}
			
			sbCourse.append(tempDTO.getSQLStatement());
			sbCourse.append(SQLConstant.BRACKET_RIGHT);
		}
		
		VstudentcourseDTO searchDTO = new VstudentcourseDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement()+ sbCourse.toString());
		
		List<VstudentcourseDTO> studentCourseArray = 
				queryDAO.queryForObjectList(VstudentcourseDTO.SQL_SEARCH, searchDTO);
		
		List<StudentCourseInfoDTO> studentCourseInfoArray = new ArrayList<StudentCourseInfoDTO>();
		for(VstudentcourseDTO item: studentCourseArray){
			StudentCourseInfoDTO courseInfo = new StudentCourseInfoDTO();
			courseInfo.setCourseEnd(item.getcourseend());
			courseInfo.setCourseID(item.getcourseid());
			courseInfo.setCourseName(item.getcoursename());
			courseInfo.setCoursePeriod(item.getcourseperiod());
			courseInfo.setCourseStart(item.getcoursestart());
			courseInfo.setCourseTypeC(item.getcoursetypec());
			courseInfo.setCourseTypeP(item.getcoursetypep());
			courseInfo.setOrderID(item.getorderid());
			courseInfo.setPath(item.getpath());
			/****************qindong 20141229 start******************/
			
			StudyhistoryDTO sDTO=new StudyhistoryDTO();
			sDTO.setorderid(item.getorderid());
			sDTO.setcourseid(item.getcourseid());
			List<StudyhistoryDTO> listsDTO=queryDAO.queryForObjectList(StudyhistoryDTO.EQUAL_SEARCH, sDTO);
			
			/****************qindong 20141229 end********************/
			//if(listsDTO.size()==0){
			if(item.getstate() == Integer.parseInt(LEARNING_STATE.NOT_GENERATE.toString()) || item.getstate() == Integer.parseInt(LEARNING_STATE.NOT_OPENED.toString())){
				courseInfo.setIsFrozen(null);
			}else{
				if(item.getstate() == Integer.parseInt(LEARNING_STATE.FROZEN.toString()) || item.getstate() == Integer.parseInt(LEARNING_STATE.TO_THAW.toString())){
					courseInfo.setIsFrozen(true);
				}
				else{
					courseInfo.setIsFrozen(false);
				}
			}
			courseInfo.setTeacherName(item.getteachername());
			studentCourseInfoArray.add(courseInfo);
		}
		
		return studentCourseInfoArray.toArray(new StudentCourseInfoDTO[studentCourseInfoArray.size()]);
	}
	
	private CodeWithValueDTO[] getCourseType(Integer studentID){
		VstudentcoursegroupDTO search = new VstudentcoursegroupDTO();
		search.setstudentid(studentID);
		List<VstudentcoursegroupDTO> studentCourseArray = 
				queryDAO.queryForObjectList(VstudentcoursegroupDTO.EQUAL_SEARCH, search);

		List<CodeWithValueDTO> courseTypes = new ArrayList<CodeWithValueDTO>();
		for(VstudentcoursegroupDTO item: studentCourseArray){
			CodeWithValueDTO code = new CodeWithValueDTO();
			code.setCode(item.getcoursetypec().toString());
			code.setValue(item.getcatagorycname());
			courseTypes.add(code);
		}
		
		return courseTypes.toArray(new CodeWithValueDTO[courseTypes.size()]);
	}
}
